package com.ruoyi.make.service.impl;

import java.util.List;

import com.ruoyi.make.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.make.mapper.ScllProductionMaterialRequisitionMapper;
import com.ruoyi.make.service.IScllProductionMaterialRequisitionService;

import javax.annotation.Resource;

/**
 * 生产领料Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-16
 */
@Service
public class ScllProductionMaterialRequisitionServiceImpl implements IScllProductionMaterialRequisitionService 
{
    @Resource
    private ScllProductionMaterialRequisitionMapper scllProductionMaterialRequisitionMapper;

    /**
     * 查询生产领料
     * 
     * @param scllId 生产领料主键
     * @return 生产领料
     */
    @Override
    public ScllProductionMaterialRequisition selectScllProductionMaterialRequisitionByScllId(Long scllId)
    {
        return scllProductionMaterialRequisitionMapper.selectScllProductionMaterialRequisitionByScllId(scllId);
    }

    /**
     * 查询生产领料列表
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 生产领料
     */
    @Override
    public List<ScllProductionMaterialRequisition> selectScllProductionMaterialRequisitionList(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        return scllProductionMaterialRequisitionMapper.selectScllProductionMaterialRequisitionList(scllProductionMaterialRequisition);
    }

    /**
     * 新增生产领料
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScllProductionMaterialRequisition(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        int rows = scllProductionMaterialRequisitionMapper.insertScllProductionMaterialRequisition(scllProductionMaterialRequisition);
        insertLldWlAndDw(scllProductionMaterialRequisition);
        scllProductionMaterialRequisitionMapper.updateLlStatus(scllProductionMaterialRequisition.getGdCode());
        return rows;
    }


    /**
     * 查询哪些仓库有这个物料
     *
     * @param wlCode 生产领料
     * @return 生产领料集合
     */
    @Override
    public List<CkAndKcxq> CkByWlCode(String wlCode){
        return scllProductionMaterialRequisitionMapper.CkByWlCode(wlCode);
    }

    /**
     * 领料
     *
     * @param scllMaterialRequiredRescord 领料
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScllMaterialRequiredRescord(List<ScllMaterialRequiredRescord> scllMaterialRequiredRescord)
    {
        for (ScllMaterialRequiredRescord row:scllMaterialRequiredRescord){
            scllProductionMaterialRequisitionMapper.insertScllMaterialRequiredRescord(row);
        }
        scllProductionMaterialRequisitionMapper.updateLldState(scllMaterialRequiredRescord.get(1).getScllId());
        return 1;
    }

    @Transactional
    @Override
    public int auditMaterialRequisition(Long scllId) {
        List<ScllMaterialRequiredRescord> rescords=scllProductionMaterialRequisitionMapper.selectRequiredMaterialByScllId(scllId);
        for (ScllMaterialRequiredRescord row:rescords){
            List<KcxqInventoryDetails> kcxqInventoryDetails=scllProductionMaterialRequisitionMapper.selectInventoryDetailsByWlAndCk(row.getWlCode(),row.getCkCode());
            for (KcxqInventoryDetails rowTwo:kcxqInventoryDetails){
                if (rowTwo.getHaveNum()>row.getGetNum()){
                    rowTwo.setHaveNum(rowTwo.getHaveNum()-row.getGetNum());
                    row.setLlCode(rowTwo.getLlCode());
                    row.setLlslLevel(rowTwo.getWlLevel());
                    scllProductionMaterialRequisitionMapper.updateInventoryDetailsHaveNum(rowTwo);
                    break;
                }
            }
            row.setScllId(scllId);
            scllProductionMaterialRequisitionMapper.updateMaterialRequiredRescordLlCode(row);
            System.out.println("======"+row);
        }
        scllProductionMaterialRequisitionMapper.updateMaterialRequisitionState(scllId);
        return 1;
    }



    /**
     * 查询生产领料单
     *
     * @param lldh 生产领料
     * @return 生产领料集合
     */
    @Override
    public List<LldWlAndDw> selectLldWlAndDwList(String lldh) {
        LldWlAndDw lldWlAndDw=new LldWlAndDw();
        lldWlAndDw.setLldh(lldh);
        return scllProductionMaterialRequisitionMapper.selectLldWlAndDwList(lldWlAndDw);
    }

    /**
     * 修改生产领料
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScllProductionMaterialRequisition(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        scllProductionMaterialRequisitionMapper.deleteLldWlAndDwByLldh(scllProductionMaterialRequisition.getScllId());
        insertLldWlAndDw(scllProductionMaterialRequisition);
        return scllProductionMaterialRequisitionMapper.updateScllProductionMaterialRequisition(scllProductionMaterialRequisition);
    }

    /**
     * 批量删除生产领料
     * 
     * @param scllIds 需要删除的生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScllProductionMaterialRequisitionByScllIds(Long[] scllIds)
    {
        scllProductionMaterialRequisitionMapper.deleteLldWlAndDwByLldhs(scllIds);
        return scllProductionMaterialRequisitionMapper.deleteScllProductionMaterialRequisitionByScllIds(scllIds);
    }

    /**
     * 删除生产领料信息
     * 
     * @param scllId 生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScllProductionMaterialRequisitionByScllId(Long scllId)
    {
        scllProductionMaterialRequisitionMapper.deleteLldWlAndDwByLldh(scllId);
        return scllProductionMaterialRequisitionMapper.deleteScllProductionMaterialRequisitionByScllId(scllId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param scllProductionMaterialRequisition 生产领料对象
     */
    @Transactional
    public void insertLldWlAndDw(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        List<LldWlAndDw> lldWlAndDwList = scllProductionMaterialRequisition.getLldWlAndDwList();
        String lldh = scllProductionMaterialRequisition.getLldh();
        if (StringUtils.isNotNull(lldWlAndDwList))
        {
            List<LldWlAndDw> list = new ArrayList<LldWlAndDw>();
            for (LldWlAndDw lldWlAndDw : lldWlAndDwList)
            {
                lldWlAndDw.setLldh(lldh);
                list.add(lldWlAndDw);
            }
            if (list.size() > 0)
            {
                scllProductionMaterialRequisitionMapper.batchLldWlAndDw(list);
            }
        }
    }
}
